/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.surveilans.controller;

import com.surveilans.classes.SMS;
import com.surveilans.model.SMSModel;
import java.util.*;
import javax.servlet.*;

/**
 *
 * @author Master
 */
public class SMSController implements ServletContextListener {
    SMSModel sms = new SMSModel();
    @Override
    public void contextInitialized(ServletContextEvent event) {
        ServletContext servletContext = event.getServletContext();
        try {
            // create the timer and timer task objects
            Timer timer = new Timer();
            SMSRetriever task = new SMSRetriever(); //this class implements Callable.

            // get a calendar to initialize the start time
            Calendar calendar = Calendar.getInstance();
            Date startTime = calendar.getTime();

            // schedule the task to run hourly
            timer.scheduleAtFixedRate(task, startTime, 1000 * 20);

            // save our timer for later use
            servletContext.setAttribute("timer", timer);
        } catch (Exception e) {
            servletContext.log("Problem initializing the task that was to run hourly: " + e.getMessage());
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
    }
    
    public class SMSRetriever extends TimerTask {

        @Override
        public void run() {
            ArrayList<SMS> as = sms.getNewSMS();
            Iterator itr = as.iterator();
            while (itr.hasNext()) {
                SMS s = (SMS) itr.next();
                System.out.println("SMS: "+s.getId()+" -> "+s.getFrom()+" : "+s.getText());
                String text = s.getText().trim().toLowerCase();
                String[] args = text.split(" ");
                if(text.isEmpty()){
                    sms.smsError(s.getFrom());
                }else if(text.equalsIgnoreCase("help")){
                    sms.sendSMS(s.getFrom(),"ketik HELP DATA untuk bantuan data surveilans\natau HELP INFO untuk bantuan info surveilans");
                }else if(args.length < 2){
                    sms.smsError(s.getFrom());
                }else if(args[0].equalsIgnoreCase("data")){
                    //data surveilans
                    if(args.length != 16){
                        sms.smsError(s.getFrom());
                    }else{
                        sms.processData(s.getFrom(),s.getText());
                    }
                }else if(args[0].equalsIgnoreCase("info")){
                    //info surveilans
                    if(args.length != 4){
                        sms.smsError(s.getFrom());
                    }else{
                        sms.processInfo(s.getFrom(),s.getText());
                    }
                }else if(args[0].equalsIgnoreCase("help")){
                    if(args.length != 2){
                        sms.smsError(s.getFrom());
                    }else if(args[2].equalsIgnoreCase("data")){
                        //help data
                    }else if(args[2].equalsIgnoreCase("info")){
                        //help info
                    }else{
                        sms.smsError(s.getFrom());
                    }
                }else{
                    sms.smsError(s.getFrom());
                }
                sms.setSMSProcessed(s.getId());
            }
        }
    }
}
